/**
 * 
 */
package com.clh.service.impl;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Random;
import java.util.UUID;

import com.clh.common.PageInfo;
import com.clh.dao.OwnerDao;
import com.clh.dao.PayDao;
import com.clh.dao.impl.OwnerDaoImpl;
import com.clh.dao.impl.PayDaoImpl;
import com.clh.entity.Complaint;
import com.clh.entity.Owner;
import com.clh.entity.Pay;
import com.clh.service.PayService;

/**
 * @Description: TODO
 * @author 陈志艺
 * @date 2022年5月31日 下午5:47:57
 * @version v1.0
 */
public class PayServiceImpl implements PayService {
	private PayDao payDao = new PayDaoImpl();

	@Override
	public List<Pay> getPayList() {
		List<Pay> list = payDao.find();
		return list;
	}

	@Override
	public int deleteByPayNo(String payNo) {
		return payDao.deleteByNo(payNo);
	}

	@Override
	public PageInfo<Pay> getPayListByPageInNOAndType(String start, String end, String payTypeId, String roomNo,
			int pageNum, int pageSize) {
		if(roomNo!=null&&!"".equals(roomNo)) {
			roomNo=new StringBuffer().append(roomNo).toString();
		}
		// 调用订单表的持久层-多条件分页查询
				List<Pay> list =payDao.findByPageInNoPay(start, end, payTypeId, roomNo, pageNum, pageSize);
				// 调用订单表的持久层-多条件查询总记录数
				Integer total =payDao.countByConditionByType(start, end, payTypeId, roomNo);
				// 封装分页对象
				PageInfo<Pay> pageInfo = new PageInfo<Pay>(list, pageNum, pageSize, total);
				return pageInfo;
	}
	@Override
	public PageInfo<Pay> getPayListByPage(String start, String end, String roomNo, int pageNum, int pageSize) {
		if(roomNo!=null&&!"".equals(roomNo)) {
			roomNo=new StringBuffer().append(roomNo).toString();
		}
		// 调用订单表的持久层-多条件分页查询
				List<Pay> list =payDao.findByPage(start, end, roomNo, pageNum, pageSize);
				// 调用订单表的持久层-多条件查询总记录数
				Integer total =payDao.countByCondition(start, end, roomNo);
				// 封装分页对象
				PageInfo<Pay> pageInfo = new PageInfo<Pay>(list, pageNum, pageSize, total);
				return pageInfo;
	}

	

	@Override
	public boolean addPay(Pay pay) {
		// 设置商品编号UUID
		UUID uuid = UUID.randomUUID();
		String payNo = uuid.toString().replace("-", "");
		System.out.println(payNo);
		pay.setPayNo(payNo);
		// 2、设置商品创建时间
		Date date = new Date();
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");// 设置日期格式
		String now = df.format(date);
		pay.setPayStartTime(now);
		Date newDate = stepMonth(date, 3);
		String end=df.format(newDate);
		pay.setPayEndTime(end);
		// 调用持久层执行插入操作
		return payDao.insert(pay) > 0;
	}

	/**根据生成日期，往后3个月生成最后缴纳日期的方法
	 * @param date	当前日期
	 * @param month	往后的时间
	 * @return
	 */
	private Date stepMonth(Date date, int month) {
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		c.add(Calendar.MONTH, month);

		return c.getTime();
	}

	@Override
	public Pay getPayById(String payNo) {
		// TODO Auto-generated method stub
		return payDao.findByNo(payNo);
	}

	@Override
	public boolean modifyPay(Pay pay) {
		// TODO Auto-generated method stub
		return payDao.update(pay)>0;
	}

	@Override
	public boolean isExist(String repeatPay) {
		
		return payDao.isExist(repeatPay);
	}

	@Override
	public boolean propertyGenerate(String year,String month) {
		
		//1、查询业主表，获取业主列表（至少包含房号和面积信息）
		OwnerDao ownerDao = new OwnerDaoImpl();
		PayDao payDao = new PayDaoImpl();
		List<Owner> list = ownerDao.getAllOwners();
		//2、遍历list，生成物业费订单
		int count = 0;
		int n = 0;
		for(Owner o:list) {
			//生成缴费对象
			Pay pay = new Pay();
			//缴费单号构成：年+月+缴费类型(1)+房号
			StringBuilder payNo = new StringBuilder();
			payNo.append(year).append(Integer.valueOf(month)>10?month:"0"+month).append(1)
			.append(o.getRoomNo().length()>3?o.getRoomNo():"0"+o.getRoomNo());
			pay.setPayNo(payNo.toString());
			
			pay.setRoomNo(o.getRoomNo());
			//生成付款起始时间和终止时间 三个月一交
			Calendar startCalendar = Calendar.getInstance();
			startCalendar.set(Integer.valueOf(year), Integer.valueOf(month)-1, 1);//Calendar月份从0开始
			Date startDate = startCalendar.getTime();
			startCalendar.add(Calendar.MONTH, 3);
			Date endDate = startCalendar.getTime();
			SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
			String startDate1 = df.format(startDate);
			String endDate1 = df.format(endDate);
			pay.setPayStartTime(startDate1);
			pay.setPayEndTime(endDate1);
			//生成物业费 5元/平米
			BigDecimal aver = new BigDecimal(5);
			pay.setPayMoney(o.getRoomArea().multiply(aver));
			pay.setPayStatus(0);
			//缴费类型
			pay.setPayTypeId(1);
			
			
			//写入缴费表
			n = payDao.insert(pay);
			if(n>0) {
				count++;
			}
		}
		//查看物业费是否有足够条数
		if(count==list.size()) {
			return true;
		}
		return false;
	}

	@Override
	public boolean waterGenerate(String year, String month) {
		//1、查询业主表，获取业主列表（至少包含房号和面积信息）
		OwnerDao ownerDao = new OwnerDaoImpl();
		PayDao payDao = new PayDaoImpl();
		List<Owner> list = ownerDao.getAllOwners();
		
		//2、遍历list，生成物业费订单
		int count = 0;
		int n = 0;
		Random r = new Random();
		for(Owner o:list) {
			//生成缴费对象
			Pay pay = new Pay();
			//缴费单号构成：年+月+缴费类型(1)+房号
			StringBuilder payNo = new StringBuilder();
			payNo.append(year).append(Integer.valueOf(month)>10?month:"0"+month).append(2)
			.append(o.getRoomNo().length()>3?o.getRoomNo():"0"+o.getRoomNo());
			pay.setPayNo(payNo.toString());
			pay.setRoomNo(o.getRoomNo());
			//生成付款起始时间和终止时间
			Calendar startCalendar = Calendar.getInstance();
			startCalendar.set(Integer.valueOf(year), Integer.valueOf(month)-1, 1);//Calendar月份从0开始
			Date startDate = startCalendar.getTime();
			startCalendar.add(Calendar.MONTH, 1);
			Date endDate = startCalendar.getTime();
			SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
			String startDate1 = df.format(startDate);
			String endDate1 = df.format(endDate);
			pay.setPayStartTime(startDate1);
			pay.setPayEndTime(endDate1);
			//水费 5元/吨
			BigDecimal aver = new BigDecimal(5);
			//2、生成3-10的随机数
			int ru = r.nextInt(10-3+1)+3;
			BigDecimal ruu = new BigDecimal(ru);
			pay.setPayMoney(ruu.multiply(aver));
			pay.setPayStatus(0);
			//缴费类型
			pay.setPayTypeId(2);
			
			
			//写入缴费表
			n = payDao.insert(pay);
			if(n>0) {
				count++;
			}
		}
		//查看水费是否有足够条数
		
		if(count==list.size()) {
			return true;
		}
		return false;
	}

	@Override
	public boolean electricGenerate(String year, String month) {
		//1、查询业主表，获取业主列表（至少包含房号和面积信息）
		OwnerDao ownerDao = new OwnerDaoImpl();
		PayDao payDao = new PayDaoImpl();
		List<Owner> list = ownerDao.getAllOwners();
		
		//2、遍历list，生成电费订单
		int count = 0;
		int n = 0;
		Random r = new Random();
		for(Owner o:list) {
			//生成缴费对象
			Pay pay = new Pay();
			//缴费单号构成：年+月+缴费类型(4)+房号
			StringBuilder payNo = new StringBuilder();
			payNo.append(year).append(Integer.valueOf(month)>10?month:"0"+month).append(4)
			.append(o.getRoomNo().length()>3?o.getRoomNo():"0"+o.getRoomNo());
			pay.setPayNo(payNo.toString());
			pay.setRoomNo(o.getRoomNo());
			//生成付款起始时间和终止时间
			Calendar startCalendar = Calendar.getInstance();
			startCalendar.set(Integer.valueOf(year), Integer.valueOf(month)-1, 1);//Calendar月份从0开始
			Date startDate = startCalendar.getTime();
			startCalendar.add(Calendar.MONTH, 1);
			Date endDate = startCalendar.getTime();
			SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
			String startDate1 = df.format(startDate);
			String endDate1 = df.format(endDate);
			pay.setPayStartTime(startDate1);
			pay.setPayEndTime(endDate1);
			//电费 0.55元/度 
			BigDecimal aver = new BigDecimal(0.55);
			//2、生成150-200的随机数
			int ru = r.nextInt(200-150+1)+150;
			BigDecimal ruu = new BigDecimal(ru);
			pay.setPayMoney(ruu.multiply(aver));
			pay.setPayStatus(0);
			//缴费类型
			pay.setPayTypeId(4);
			
			
			//写入缴费表
			n = payDao.insert(pay);
			if(n>0) {
				count++;
			}
		}
		//查看电费是否有足够条数
		
		if(count==list.size()) {
			return true;
		}
		return false;
	}

	@Override
	public boolean updatePayStatus(String payNo) {
		return payDao.updatePayStatus(payNo);
	}




	


}
